<?php
namespace Home\Controller;
use Home\Controller\BaseController;

class IndexController extends BaseController {

    public function _initialize(){
         parent::_initialize();
    }
    
    public function index(){
        //爆品推荐
        $this->assign('pc_tj_Recommend',  $this->getRecommend(1,3));
        
        //美妆推荐
        $this->assign('pc_one_rec',  $this->getRecommend(2,6));
        //母婴推荐
        $this->assign('pc_two_rec',  $this->getRecommend(3,6));
        //吃货推荐
        $this->assign('pc_three_rec',  $this->getRecommend(4,6));
        //吃货推荐
        $this->assign('pc_four_rec',  $this->getRecommend(5,6));
        //母婴推荐
        $this->assign('pc_five_rec',  $this->getRecommend(6,6));
        //吃货推荐
        $this->assign('pc_six_rec',  $this->getRecommend(7,6));
        
        
        //网站公告
        $notice = M('article')->where('cat_id=116')->limit(4)->select();
        $notice2 = M('article')->where('cat_id=117')->limit(4)->select();
        
        //获取最近购买的订单数
        $wheres['order_state'] = array('egt',20);
        $orderNotice = M('order')->field('user_id,user_name')->where($wheres)->order('order_id desc')->group('user_id')->limit(30)->select();
        foreach ($orderNotice as $key => $value) {
            $orderNotice[$key]['avatar'] = M('users')->where('user_id='.$value['user_id'])->getField('user_avatar');
        }
        $this->assign("orderNotice",$orderNotice);
        //淘生活、淘实惠
        $webModel = D('Web');
        $tddLife = $webModel->relation('webCode')->where('web_show=1 and web_type=0')->select();

        //猜你喜欢
        if(S('like_goods')){
            $recommend = S('like_goods');
        }else{
            $where['position_id'] = 1;
            $where['recommend_type'] = 2;
            $recommend = M('recommend')->where($where)->select();
            foreach ($recommend as $key => $value) {
                $where['goods_id'] = $value['goods_id'];
                $where['goods_state'] =1;
                $recommend[$key] = M('goods')->field('goods_id,goods_title,goods_thumb,goods_price,goods_markprice,goods_hits')->where($where)->find();
            }
            multi_array_sort($recommend, 'goods_hits');
            S('like_goods',$recommend, array('expire'=>7000));
        }
        $this->assign('like_goods',$recommend);
        $this->assign("tddLife",$tddLife);
        $this->assign("notice",$notice);
        $this->assign("notice2",$notice2);
        $this->display();
    }


    
    function getRecommend($rid,$limit=12){
        $maps['recommend_type'] = 2;
        $maps['position_id'] = $rid;
        $recommend_goods = M('recommend')->where($maps)->limit($limit)->select();
        foreach ($recommend_goods as $key => $value) {
            $where['goods_id'] = $value['goods_id'];
            $where['goods_sale'] =1;
            $where['goods_state'] =1;
            $re[$key] = M('goods')->field('goods_id,goods_title,goods_thumb,goods_price,goods_markprice,goods_hits')->where($where)->find();
            if(empty($re[$key])){
                unset($recommend_goods[$key]);
            }else{
                $recommend_goods[$key] =$re[$key];
            }
        }
        multi_array_sort($recommend_goods, 'goods_hits');
        return $recommend_goods;
    }

//    public function del(){
//        $store = M('store')->field('store_id,store_banner,store_slide')->select();
//        foreach($store as $v)
//        {
//            if(trim($v['store_banner']) == 'http://img.taodudu.com.cn/2016/1213/02/d4a239fe46999ece0c8ead59c05043bf.jpg' && trim($v['store_slide']) == 'a:1:{i:0;s:75:"http://img.taodudu.com.cn/2016/1213/11/e7ee621c3b49d257fcaa55a9ee518875.jpg";}'){
//                $data['store_banner'] = null;
//                $data['store_slide'] = null;
//            }else if(trim($v['store_banner']) == 'http://img.taodudu.com.cn/2016/1213/02/d4a239fe46999ece0c8ead59c05043bf.jpg'){
//                $data['store_banner'] = null;
//            }else if(trim($v['store_slide']) == 'a:1:{i:0;s:75:"http://img.taodudu.com.cn/2016/1213/11/e7ee621c3b49d257fcaa55a9ee518875.jpg";}'){
//                $data['store_slide'] = null;
//            }
//            if($data != null){
//                M('store')->where(array('store_id'=>$v['store_id']))->data($data)->save();
//                $data = null;
//            }
//
//        }
//    }

}